<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
include_once( APP . "Config/your_define.php" );
App::uses('Sanitize', 'Utility');
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

	var $helpers = array('Html', 'Paginator', 'Session', 'Js', 'Form', 'Cache', 'Text', 'Time');
    var $components = array('Acl', 'Session', 'Auth', 'RequestHandler', 'Cookie', 'Common', "File");

	var $layout = 'admin';

    /**
	 *
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function beforeFilter() {

		// duoc phep truy cap tat ca
		//$this->Auth->allow();

		// Truy cap theo action
		$allowActs = $this->allowActions ? $this->allowActions : array();
		$this->Auth->allowedActions = $allowActs;


		 // dieu kien dang nhap thanh cong
        $this->Auth->userScope = array( 'User.status' => array(4) );


//        //Configure AuthComponent
//        $this->Auth->authorize = 'actions';
//
//        $this->Auth->loginError = __( "Đăng nhập thất bại. Xin kiểm tra lại thông tin đăng nhập.", true );
//
//        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login' );
//        $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'home');
//        $this->Auth->loginRedirect = "/";

		// Active menu
		$name = strtolower( $this->name );
		$this->set('menu', $name);

		$user = $this->Auth->user();

        $this->set('global_user', $user);

		// Current company
		$comp = $this->Session->read( 'current_company' );
		$this->set('current_company', $comp);

		// Company list
		$company = array();
		if( $this->Session->check( 'company_list' ) )
		{
			$company = $this->Session->read( 'company_list' );
		} else {
			$com = $this->Auth->user('company_id');
            if(  !in_array( $this->Auth->user('role'), array( 1, 2 ) ) )
            {
            	if(!empty( $com )) {
                    $com = explode(',', $com);
            	}

            	$cond['id'] =  $com ;
            }

			App::uses('Company', 'Model');
			$Company = new Company();

            $company = $Company->find( 'list', array(
                'conditions' => $cond,
                'fields' => array( 'id', 'name' ) ) );

			// neu ko co company thi ko cho dang nhap vao
            if(empty($company))
            {
            	//$this->redirect('/admin/login');
            }

            $this->Session->write( 'company_list', $company );
		}
		
		$this->set('company_list', $company);
		
		// Set als
		$model = Inflector::singularize($this->name);
		$this->als = $model;
		$this->set('als', $model);
	}

	/**
     * Upload file
     *
     * @param FileData  meta
     * @return type  meta
     * @access public
     */
    function _upload($file_data) {
        // Upload avatar
        if (!empty($file_data) && $file_data['error'] == 0) {
            $file = $this->File->Upload($file_data);

            $this->File->create_thumb($file);

            return $file;
        } else {
            return '';
        }
    }

    /**
	 * Delete, Lock, Unlock
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_changeStatus( $id = null, $status = 5 ) {

		// Get model tu controller
		$model = Inflector::singularize($this->name);

		$liststatus = array(
			4 => 'Unlock',
			5 => 'Lock',
			9 => 'Delete'
		);

		if( in_array( $this->Auth->user('role'), array( 1,2) ) ) {

			if(empty( $id )) {
				$this->Session->setFlash( __('%s member Fail. User Not Found.', $liststatus[$status]), 'error');
			} else {
				$this->$model->id = (int)$id;

				if( $this->$model->saveField('status', (int)$status ) ) {
					$this->Session->setFlash( __('%s member success.', $liststatus[$status]), 'success');
				} else {
					$this->Session->setFlash( __('%s member fail.', $liststatus[$status]), 'error');
				}
			}
		} else {
			$this->Session->setFlash( 'You are not allowed to execute the function of this.', 'error');
		}

		$this->redirect('/admin/'.$this->name);
	}

	/**
     * Response for client
     *
     * @param type  meta
     * @return type  meta
     * @access public
     */
    function _responseJson($data) {
        $this->RequestHandler->setContent('json', 'text/x-json');

        echo json_encode($data);

        exit;
    }
}
